package com.aipolice.web;


import com.aipolice.service.UserService;
import com.aipolice.utils.ImageUtil;
import com.aipolice.utils.ImgInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.channels.FileLockInterruptionException;
import java.util.UUID;

/**
 * 上传文件本地版本
 * 注意项目路径不能有中文路径
 */
@Controller
public class ImageUploadController {

    @RequestMapping("/upload")
    @ResponseBody
    public ImgInfo setImgUrl(MultipartFile image, HttpServletResponse response, HttpServletRequest request, Integer isDp)
            throws Exception {
        File imageFolder= new File(request.getServletContext().getRealPath("img/article"));
        File file = new File(imageFolder,UUID.randomUUID()+".jpg");
        System.out.println(request.getServletContext().getRealPath("img/news"));
        if(!file.getParentFile().exists())
            file.getParentFile().mkdirs();
        image.transferTo(file);
        BufferedImage img = ImageUtil.change2jpg(file);
        ImageIO.write(img, "jpg", file);

        String value = "img/article/"+file.getName();

        System.out.println(value);
        String[] values = { value };

        ImgInfo imgInfo = new ImgInfo();
        imgInfo.setError(0);
        imgInfo.setUrl(values);

        System.out.println(imgInfo.toString());
        return imgInfo;
    }






}